﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;
using Actuals.CoreDomain.Data;
using Actuals.Data;
using Actuals.Common;

namespace Actuals.Business
{
    public class BusinessSprint : IBusinessSprint
    {

        private IDalSprint _dalSprint;

        public BusinessSprint(IDalSprint dalSprint)
        {
            _dalSprint = dalSprint;
        }

        public BusinessSprint()
        {
            _dalSprint = new DalSprint();
        }


        public ISprint GetSprint(int sprintId)
        {
            return _dalSprint.GetSprint(sprintId);
        }

        public ISprint GetSprintWithRelease(int sprintId, int releaseId)
        {
            return _dalSprint.GetSprintWithRelease(sprintId, releaseId);
        }

        public IList<ISprint> GetSprints(int projectId, int releaseid)
        {
            return _dalSprint.GetSprints(projectId, releaseid);
        }
        public ISprint GetSprintWithSprintId(int sprintId)
        {
            return _dalSprint.GetSprint(sprintId);
        }

        /*   public IList<ISprint> GetCurrentSprints(int projectId, int releaseid)
           {
               throw new NotImplementedException();
           }*/

        public int AddSprint(ISprint sprint)
        {
            return _dalSprint.AddSprint(sprint);
        }
        public int DeleteSprint(int sprintId)
        {
            return _dalSprint.DeleteSprint(sprintId);
        }
        public IList<ISprint> GetNextSprints(int sprintId, int projectId, int releaseId)
        {

            IBusinessSprint businessSprint = new BusinessSprint();

            ISprint currentSprint = businessSprint.GetSprint(sprintId);

            DateTime currentSprintEndDate = currentSprint.SprintEndDate;

            IList<ISprint> sprints = businessSprint.GetSprints(projectId, releaseId);

            IList<ISprint> nextSprints = new List<ISprint>();

            foreach (ISprint nextSprint in sprints)
            {

                DateTime _startDate = nextSprint.SprintStartDate;

                if (_startDate >= currentSprintEndDate)
                {

                    nextSprints.Add(nextSprint);

                }

            }




            return nextSprints;

        }







        public void UpdateInCompleteTechTaskToNextSprint(int sprintId, int nextSprint)
        {

            IBusinessTechnicalTask businessInCompleteTechTasks = new BusinessTechnicalTask();
            IList<ITechnicalTask> inCompleteTechnicalTasks =
                businessInCompleteTechTasks.GetInDcompleteTechnicalTaskInCurrentSprint(sprintId);
            IList<int> distinctUserStories = inCompleteTechnicalTasks.Select(x => x.UserStoryId).Distinct().ToList();

            foreach (int distinctUserStory in distinctUserStories)
            {
                IBusinessUserStory businessUserStory = new BusinessUserStory();

                IUserStory userStory = businessUserStory.GetUserStory(distinctUserStory);

                IUserStory _userStory = new UserStory();

                _userStory.OldUserStoryId = userStory.UserStoryId;

                _userStory.TaskId = userStory.TaskId;

                _userStory.UserStoryDescription = userStory.UserStoryDescription;

                _userStory.Sprint = nextSprint.ToString();

                _userStory.StoryPoints = userStory.StoryPoints;

                businessUserStory.AddUserStory(_userStory);

                

            }


            foreach (ITechnicalTask incompleteTechnicalTask in inCompleteTechnicalTasks)
            {
                IBusinessUserStory businessUserStory = new BusinessUserStory();
                IUserStory userStory = businessUserStory.GetOldUserStory(incompleteTechnicalTask.UserStoryId);

                incompleteTechnicalTask.UserStoryId = userStory.UserStoryId;

                incompleteTechnicalTask.TechnicalTaskId = incompleteTechnicalTask.TechnicalTaskId;

                incompleteTechnicalTask.SprintId = nextSprint;

                businessInCompleteTechTasks.UpdateIncompleteTechTasks(incompleteTechnicalTask);

            }

        }
        public ISprint GetCurrentSprint(int projectId, int releaseId)
        {
            IList<ISprint> sprints = GetSprints(projectId, releaseId);
            ISprint currentSprint = new Sprint();
            foreach (ISprint _currentSprint in sprints)
            {
                int _status = _currentSprint.Status;
                if (_status == Constants.SprintStatus.InProgress)
                {
                    currentSprint = _currentSprint;
                }
            }
            return currentSprint;
        }


        public IList<ISprint> GetSprintsWithoutCurrentSprint(int projectId, int releaseId)
        {
            ISprint currentSprint = GetCurrentSprint(projectId, releaseId);
            IList<ISprint> sprints = GetSprints(projectId, releaseId);
            IList<ISprint> SprintNoCurrentSprint = new List<ISprint>();
            foreach (ISprint sprint in sprints)
            {
                int _sprintId = currentSprint.SprintId;
                if (sprint.SprintId != _sprintId)
                {
                    SprintNoCurrentSprint.Add(sprint);
                }
            }

            return SprintNoCurrentSprint;
        }














    }
}
